package main.math;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;

import main.entity.LargeNumber;


public class ConvertUtils {

	public static String getFamiliarName(BigDecimal dec){
		String retorno = "";
		BigDecimal thousand = new BigDecimal("1000");
		BigDecimal tenThousand = new BigDecimal("10000");
		
			boolean end = false;
			BigDecimal temp = dec;
			int cont = 0;
			while (!end){
				
				if (temp.compareTo(tenThousand)<=0) {
					retorno = format(temp,LargeNumber.getLargeNumber(cont).getPrintValue());
					end = true;
				} else {
					temp = temp.divide(thousand);
				}
				cont ++;
			}
		
		
		
		return retorno;
		
	}
	
	public static BigDecimal getSuperRounded(BigDecimal dec){
		BigDecimal retorno =new BigDecimal("0");;
		BigDecimal thousand = new BigDecimal("1000");
		BigDecimal hundredThousand = new BigDecimal("100000");
		
		boolean end = false;
		BigDecimal temp = dec;
		int cont = 0;
		while (!end){
			
			if (temp.compareTo(hundredThousand)<=0) {
				retorno = temp.setScale(0,RoundingMode.HALF_EVEN).multiply(thousand.pow(cont));
				end = true;
			} else {
				temp = temp.divide(thousand);
			}
			cont ++;
		}
		
		
		
		return retorno;
		
	}

	private static String format(BigDecimal dec, String printValue) {
		NumberFormat nf = NumberFormat.getIntegerInstance();
		dec.setScale(0, RoundingMode.FLOOR).toPlainString();
		String retorno = dec.setScale(0, RoundingMode.FLOOR).toPlainString() + " "+printValue;
		
		return retorno;
	}
	
	public static BigDecimal parseNotadedValue(String text){
		BigDecimal thousand = new BigDecimal("1000");
		BigDecimal dec = new BigDecimal("0");

		String modifier = text.substring(text.length()-2).replaceAll("\\d", "");
		modifier = modifier.trim();
		
		String value = text.replaceAll("\\D", "");
		value = value.trim();
		
		BigDecimal bdValue = new BigDecimal(value);
		
		dec = bdValue;
		
		LargeNumber l = LargeNumber.getLargeNumber(modifier);
		
		if (l!=null) {
			dec = dec.multiply(thousand.pow(l.getOrder()));
		}
		
		return dec;
	}
	
}
